package com.ydlclass.transaction;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration
@EnableAspectJAutoProxy
@EnableTransactionManagement
public class AppConfiguration {

	@Bean
	public DataSource dataSource() {
		// 注入一个hikari的数据源
		HikariDataSource hikariDataSource = new HikariDataSource();
		hikariDataSource.setUsername("root");
		hikariDataSource.setPassword("root");
		hikariDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/ydlclass?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8");
		hikariDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
		return hikariDataSource;
	}

	@Bean
	public JdbcTemplate jdbcTemplate() {
		JdbcTemplate jdbcTemplate = new JdbcTemplate();
		jdbcTemplate.setDataSource(dataSource());
		return jdbcTemplate;
	}

	@Bean
	public PlatformTransactionManager transactionManager(){
		return new DataSourceTransactionManager(dataSource());
	}

}
